#include "menuTest.h"
#include <pspdisplay.h>
/*
 * menuTest.c
 *
 *  Created on: 09-oct-2008
 *      Author: hector
 */

#define printf pspDebugScreenPrintf

menu * createTestMenu(int cuantos) {
	menu * testMenu = (menu *) malloc(sizeof(menu));
	testMenu->handleX = handleXMenuTest;
	testMenu->handleC = handleCMenuTest;
	testMenu->handleS = handleSMenuTest;
	testMenu->handleT = handleTMenuTest;
	testMenu->handleUp = handleUpMenuTest;
	testMenu->handleDown = handleDownMenuTest;
	testMenu->handleLT = handleLTMenuTest;
	testMenu->handleRT = handleRTMenuTest;
	testMenu->init = init;
	testMenu->destroy = NULL;
	testMenu->aboutToHide = NULL;
	testMenu->aboutToShow = NULL;

	menuTestState * state = (void *) malloc(sizeof(menuTestState));
	state->selectedLine = 0;
	state->lines = cuantos;
	testMenu->state = (void *) state;

	return testMenu;
}

int handleXMenuTest(void *state, mpd_Connection *connection) {
	printf("X\n");
	return 1;
}

int handleTMenuTest(void *state, mpd_Connection *connection) {
	printf("T\n");
	return 1;
}

int handleSMenuTest(void *state, mpd_Connection *connection) {
	printf("S\n");
	return 1;
}

int handleCMenuTest(void *state, mpd_Connection *connection) {
	printf("C\n");
	return 1;
}

int handleUpMenuTest(void *state, mpd_Connection *connection) {
	menuTestState * menuState = (menuTestState *) state;
	menuState->selectedLine--;
	if (menuState->selectedLine < 0)
		menuState->selectedLine = 0;
	else
		paint(state, connection);
	return 1;
}

int handleDownMenuTest(void *state, mpd_Connection *connection) {
	menuTestState * menuState = (menuTestState *) state;
	if (menuState->selectedLine != (menuState->lines - 1)) {
		menuState->selectedLine++;
		paint(state, connection);
	}

	return 1;
}

int handleRTMenuTest(void*state, mpd_Connection *connection) {
	menuTestState * menuState = (menuTestState *) state;
	menuState->selectedLine += 10;
	if (menuState->selectedLine >= menuState->lines) {
		menuState->selectedLine = menuState->lines - 1;
	}

	return paint(state, connection);
}

int handleLTMenuTest(void *state, mpd_Connection *connection) {

	menuTestState * menuState = (menuTestState *) state;
	menuState->selectedLine -= 10;
	if (menuState->selectedLine < 0) {
		menuState->selectedLine = 0;
	}

	return paint(state, connection);
}

int init(void *state, mpd_Connection *connection) {
	return paint(state, connection);
}

int paint(void *state, mpd_Connection *connection) {
	int inicio = 0;
	int cuantos = 0;

	menuTestState * menuState = (menuTestState *) state;
	sceDisplayWaitVblank();
	pspDebugScreenClear();

	inicio = menuState->selectedLine - 32;
	if (inicio < 0)
		inicio = 0;

	cuantos = inicio + ((menuState->lines < 33) ? menuState->lines : 33);

	for (; inicio < cuantos; inicio++) {
		if (inicio == menuState->selectedLine) {
			pspDebugScreenSetTextColor(0x0000FF);
			printf("Linea %d-%d-%d-%d\n", inicio, cuantos,
					menuState->selectedLine, menuState->lines);
			pspDebugScreenSetTextColor(0xFFFFFF);
		} else
			printf("Linea %d-%d-%d-%d\n", inicio, cuantos,
					menuState->selectedLine, menuState->lines);
	}

	return 1;

}

